﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using cwtrock.model;

namespace cwtrock.view.handle
{
    public partial class DetailRpt : Form
    {
        private List<Vrecords> vrls;
      
        public DetailRpt()
        {
            InitializeComponent();

            //这是了隔行换色的代码  
            this.dgv_Detail.RowsDefaultCellStyle.BackColor = Color.FromArgb(255, 255, 255);//第一行  
            this.dgv_Detail.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(244, 247, 252);//第二行  
            this.dgv_Detail.GridColor = Color.FromArgb(128, 128, 128);//字段网格边框  
            dgv_Detail.Select();
        }

        public void initFrm(string acct, string duration, List<Vrecords> vrls)
        {
            acctname_txt.Text = acct;
            duration_txt.Text = duration;
            if (vrls == null || vrls.Count == 0)
                return;

            Vrecords vrecord = vrls[0];
            // 期初行
            DateTime vdate = Convert.ToDateTime(vrecord.Voucher.Vdatestr);
            DateTime first = new DateTime(vdate.Year, vdate.Month, 1);
            DateTime last = new DateTime(vdate.Year, vdate.Month, DateTime.DaysInMonth(vdate.Year,vdate.Month));
            string datestr = first.ToShortDateString();
            addGdvRows(datestr, "", "期初余额", "", "", "平", "");
            DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
            cellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgv_Detail.Rows[0].Cells[2].Style = cellStyle;
            dgv_Detail.Rows[0].DefaultCellStyle.BackColor = Color.LightYellow;

            decimal bamt_term = 0;
            decimal lamt_term = 0;

            // 数据行
            foreach (Vrecords vr in vrls)
            {
                decimal bamt = vr.Vrbamount ;
                decimal lamt = vr.Vrlamount  ;
                bamt_term += bamt;
                lamt_term += lamt;
                string bamtstr = bamt == 0 ? "" : bamt + ".00";
                string lamtstr = lamt == 0 ? "" : lamt + ".00";
                string[] remains = getRemains(bamt, lamt);
                
                DateTime voudate = Convert.ToDateTime(vr.Voucher.Vdatestr);
                addGdvRows(voudate.ToShortDateString(), vr.Voucher.Vtypename + "-" + vr.Voucher.Vcode
                                        , vr.Vrdigest, bamtstr, lamtstr, remains[0], remains[1]);
            }

              // 尾行
                string[] last_remains = getRemains(bamt_term, lamt_term);
                string bamt_termstr = bamt_term == 0 ? "" : bamt_term + ".00";
                string lamt_termstr = lamt_term == 0 ? "" : lamt_term + ".00";

                addGdvRows(last.ToShortDateString(), "", "本期合计", bamt_termstr, lamt_termstr, last_remains[0], last_remains[1]);
                addGdvRows(last.ToShortDateString(), "", "本年累计", bamt_termstr, lamt_termstr, last_remains[0], last_remains[1]);
                addGdvRows(last.ToShortDateString(), "", "结转下年", bamt_termstr, lamt_termstr, last_remains[0], last_remains[1]);

                int lastRowIndex = dgv_Detail.Rows.Count-1;
                int last3RowIndex = lastRowIndex - 3;

               for ( ; lastRowIndex > last3RowIndex; lastRowIndex--)
               {
                     dgv_Detail.Rows[lastRowIndex].Cells[2].Style = cellStyle;
                    dgv_Detail.Rows[lastRowIndex].DefaultCellStyle.BackColor = Color.LightYellow;
               }
                

                // 选中首行
                if (dgv_Detail.Rows.Count > 0)
                {
                    dgv_Detail.CurrentRow.Selected = false;
                    dgv_Detail.Rows[0].Selected = true;
                }
        }

        private void addGdvRows(string date, string vcode, string digest, string bamt, string lamt, string remain_status, string remain_amt)
        {
            int rows = dgv_Detail.Rows.Count;
            dgv_Detail.Rows.Add();            
            int j = 0;
            dgv_Detail.Rows[rows].Cells[j++].Value = date;
            dgv_Detail.Rows[rows].Cells[j++].Value = vcode;
            dgv_Detail.Rows[rows].Cells[j++].Value = digest;
            dgv_Detail.Rows[rows].Cells[j++].Value = bamt;
            dgv_Detail.Rows[rows].Cells[j++].Value = lamt;
            dgv_Detail.Rows[rows].Cells[j++].Value = remain_status;
            dgv_Detail.Rows[rows].Cells[j++].Value = remain_amt;

        }

        private string[] getRemains(decimal bamt, decimal lamt) {

            string[] remains = new string[2];
            string remain_status = "平";
            if (bamt > lamt)
                remain_status = "借";
            else if (bamt < lamt)
                remain_status = "贷";
            string remainstr;
            if ((bamt - lamt) != 0)
                remainstr = System.Math.Abs(bamt - lamt) + ".00";
            else
                remainstr = "";
            remains[0] = remain_status;
            remains[1] = remainstr;

            return remains;

        }
     
   
    }
}
